import { getGuessListAPI } from '@/services/homeApi'
import { ref, computed } from 'vue'
import type { ProductType } from '@/types/home'
export const useguess = () => {
  const GuessProductList = ref<ProductType[]>([])
  const page = ref(1)
  const size = ref(6)
  const isFinished = ref(false)
  const count = ref(1)
  const maxPage = computed(() => Math.ceil(count.value / size.value))
  // 获取猜你喜欢商品列表
  const getGuessProductList = async (page: number) => {
    const res = await getGuessListAPI(page)
    if (res.statusCode === 200) {
      GuessProductList.value = [...GuessProductList.value, ...res.data!]
      count.value = res.count!
    }
  }
  // 滚动到底部后执行的操作
  const scrolltolower = async () => {
    if (maxPage.value >= page.value) {
      await getGuessProductList(page.value)
      page.value++
    } else {
      isFinished.value = true
      console.log('没有更多数据了')
    }
  }
  return { GuessProductList, scrolltolower, isFinished, page, getGuessProductList }
}
